package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.text.ParseException;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import util.Mascaras;

public class Consultas extends JPanel{

    private JPanel areaBotoes, areaDados;
    private AreaEdicao areaEdicao;
    private JButton jbIncluir, jbAlterar, jbExcluir, jbPesquisar, jbProntuario;
    
    public Consultas() {
        super();
        //Instanciar objetos
        areaBotoes = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 0));
        areaDados = new JPanel(new BorderLayout());
        jbIncluir = new JButton("Incluir");
        jbAlterar = new JButton("Alterar");
        jbExcluir = new JButton("Excluir");
        jbPesquisar = new JButton("Pesquisar");
        jbProntuario = new JButton("Prontuário");
        areaEdicao = new AreaEdicao();
        
        //definir propriedades visuais
        
        //definir layouts
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        areaBotoes.setLayout(new GridLayout(1, 7, 10, 5));
        
        //definir listeners
        
        //adicionar objetos a conteineres
        add(areaEdicao);
        add(util.ElementoVazio.gerar(0, 5));
        add(areaBotoes);
        add(util.ElementoVazio.gerar(0, 5));
        add(areaDados);
        areaBotoes.add(new JPanel());
        areaBotoes.add(jbIncluir);
        areaBotoes.add(jbAlterar);
        areaBotoes.add(jbExcluir);
        areaBotoes.add(jbPesquisar);
        areaBotoes.add(jbProntuario);
        areaBotoes.add(new JPanel());
        
        //setar propriedades da tela e mostra-la
        setVisible(true);
    }

    private class AreaEdicao extends JPanel {

        private JTextField jtfNomePaciente, jtfNomeMedico;
        private JFormattedTextField jftfDataConsulta, jftfHoraConsulta;
        private JButton jbPesquisarPaciente, jbPesquisarMedico;
        private JLabel jlTipoConsulta;
        private JComboBox jcbTipoConsulta;
        private JPanel j1, j2, j3, j4;
        private final String[] tiposConsultas = {"Normal", "Retorno"};
        
        
        public AreaEdicao() {
            super();
            
            //Instanciar objetos
            j1 = new JPanel(new BorderLayout(5, 0));
            j2 = new JPanel(new BorderLayout(5, 0));
            j3 = new JPanel(new GridLayout(1, 6, 5, 1));
            j4 = new JPanel(new BorderLayout(5, 0));
            jtfNomePaciente = new JTextField();
            jtfNomeMedico = new JTextField();
            jftfDataConsulta = new JFormattedTextField();
            jftfHoraConsulta = new JFormattedTextField();
            jbPesquisarPaciente = new JButton(new ImageIcon("src\\media\\lupa.png"));
            jbPesquisarMedico = new JButton(new ImageIcon("src\\media\\lupa.png"));
            jlTipoConsulta = new JLabel("Tipo de consulta:", SwingConstants.RIGHT);
            jcbTipoConsulta = new JComboBox(tiposConsultas);
            
            formatarCampos();
            
            //definir propriedades visuais
            jtfNomePaciente.setBorder(javax.swing.BorderFactory.createTitledBorder("Paciente"));
            jtfNomePaciente.setEditable(false);
            jtfNomeMedico.setBorder(javax.swing.BorderFactory.createTitledBorder("Médico"));
            jtfNomeMedico.setEditable(false);
            jftfDataConsulta.setBorder(javax.swing.BorderFactory.createTitledBorder("Data da consulta"));
            jftfHoraConsulta.setBorder(javax.swing.BorderFactory.createTitledBorder("Hora da consulta"));
            
            //definir layouts
            setLayout(new GridLayout(3, 1, 5, 10));

            //definir listeners

            //adicionar objetos a conteineres
            j4.add(jlTipoConsulta, BorderLayout.WEST);
            j4.add(jcbTipoConsulta, BorderLayout.CENTER);
            j1.add(jbPesquisarPaciente, BorderLayout.WEST);
            j1.add(jtfNomePaciente, BorderLayout.CENTER);
            j2.add(jbPesquisarMedico, BorderLayout.WEST);
            j2.add(jtfNomeMedico, BorderLayout.CENTER);
//            j3.add(new JPanel());
            j3.add(jftfDataConsulta);
            j3.add(jftfHoraConsulta);
            j3.add(j4);
//            j3.add(jlTipoConsulta);
//            j3.add(jcbTipoConsulta);
//            j3.add(new JPanel());
            add(j1);
            add(j2);
            add(j3);
            
            //setar propriedades da tela e mostra-la
            setVisible(true);            
        }

        private void formatarCampos() {
            try {
                Mascaras.maskData(jftfDataConsulta);
                Mascaras.maskHora(jftfHoraConsulta);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
    }
    
}
